//
//  GYLockMapViewController.m
//  LockManagement
//
//  Created by apple on 2018/7/26.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYLockMapViewController.h"
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import "UIButton+GYBtn.h"
#import <BaiduMapAPI_Location/BMKLocationComponent.h>

#import "GYLock.h"

@interface GYLockMapViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate>

/** 当前地图对象 */
@property (nonatomic, strong) BMKMapView *mapView;

/** 百度定位 */
@property (nonatomic, strong) BMKLocationService *locationService;

/** 当前位置信息 */
@property (nonatomic, strong) BMKUserLocation *userLocation;
@end

@implementation GYLockMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(planingRoute:) name:@"planningRoute" object:nil];
    
    [self setupUI];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    self.mapView.frame = self.view.bounds;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.mapView.delegate = self;
    self.locationService.delegate = self;
   
    //每次进来重新刷新这个大头针
    NSString *filePath = [NSString CachesDir:@"lock.plist"];
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
    if (dic != nil) {
        NSArray *data = [dic objectForKey:@"data"];
        NSArray *tempArray = [GYLock jsonLockListData:data];
        //1.首先移除地图上的大头针,重新创建然后刷新
        [self.mapView removeAnnotations:self.mapView.annotations];
        
        for (GYLock *lock in tempArray) {
            BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
            annotation.coordinate = CLLocationCoordinate2DMake(lock.lat, lock.lng);
            annotation.title = lock.lockName;
            [self.mapView addAnnotation:annotation];
        }
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    self.mapView.delegate = nil;
    self.locationService.delegate = nil;
}

- (void)setupUI {
    //初始化地图
    self.mapView = [[BMKMapView alloc] initWithFrame:CGRectZero];
    self.mapView.showsUserLocation = NO;
    self.mapView.userTrackingMode = BMKUserTrackingModeFollow;
    self.mapView.showsUserLocation = YES;
    [self.view addSubview:self.mapView];
    
    //开始定位
    self.locationService = [[BMKLocationService alloc] init];
    [self.locationService startUserLocationService];
}


//- (void)planingRoute:(NSNotification *)noti {
//    GYLock *model = noti.object;
//    //初始化调启导航时的参数管理类
//    BMKNaviPara *para = [[BMKNaviPara alloc]init];
//    //实例化线路检索节点信息类对象
//    BMKPlanNode *end = [[BMKPlanNode alloc]init];
//    //指定终点经纬度
//    end.pt = CLLocationCoordinate2DMake(model.lat, model.lng);
//    //指定终点名称
//    end.name = model.lockName;
//    //指定终点
//    para.endPoint = end;
//    //指定返回自定义scheme
//    para.appScheme = @"baidumapsdk://mapsdk.baidu.com";
//    //应用名称
//    para.appName = @"";
//    
//    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"打开百度地图客户端" message:@"百度地图-步行导航" preferredStyle:UIAlertControllerStyleAlert];
//    UIAlertAction *openAction = [UIAlertAction actionWithTitle:@"打开" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        /**
//         调起百度地图客户端步行导航界面
//         
//         @param para 调起步行导航时传入的参数
//         @return 结果状态码
//         */
//        [BMKNavigation openBaiduMapWalkNavigation:para];
//    }];
//    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleDefault handler:nil];
//    [alert addAction:openAction];
//    [alert addAction:cancelAction];
//    [self presentViewController:alert animated:YES completion:nil];
//}


- (void)dealloc {
    self.mapView.delegate = nil;
    self.locationService.delegate = nil;
}

#pragma mark -- 关于地图的一些操作

- (void)scaleMap:(UIButton *)sender {
    //1.获取当前地图的缩放级别
    float zoomLevel = self.mapView.zoomLevel;
    if (sender.tag == 1) {
        NSLog(@"缩小地图");
        if (zoomLevel == 4) {
            [SVProgressHUD showInfoWithStatus:@"已缩放到最小!"];
            return;
        }
        [self.mapView setZoomLevel:zoomLevel-1];
    }else {
         NSLog(@"方法地图");
        if (zoomLevel == 21) {
            [SVProgressHUD showInfoWithStatus:@"已放大到最大!"];
            return;
        }
        [self.mapView setZoomLevel:zoomLevel+1];
    }
}

#pragma mark -- BMKMapViewDelegate BMKLocationServiceDelegate

/**
 *地图初始化完毕时会调用此接口
 *@param mapView 地图View
 */
- (void)mapViewDidFinishLoading:(BMKMapView *)mapView {
    NSLog(@"地图初始化完毕...");
    //在这里添加按钮
    UIButton *smallBtn = [UIButton buttonWithFrame:CGRectMake(self.view.width-50, self.view.height-60, 40, 40) backGroundImage:@"jian" tag:1 target:self action:@selector(scaleMap:)];
    [self.view addSubview:smallBtn];
    
    UIButton *largeBtn = [UIButton buttonWithFrame:CGRectMake(self.view.width-50, self.view.height-120, 40, 40) backGroundImage:@"jia" tag:2 target:self action:@selector(scaleMap:)];
     [self.view addSubview:largeBtn];
    
}

- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation {
    NSLog(@"用户更新位置成功....");
    self.userLocation = userLocation;
    [self.mapView updateLocationData:userLocation];
    //[self.mapView setCenterCoordinate:userLocation.location.coordinate];
}

- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
        BMKPinAnnotationView*annotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
        if (annotationView == nil) {
            annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
        }
        annotationView.pinColor = BMKPinAnnotationColorPurple;
        annotationView.canShowCallout= YES;      //设置气泡可以弹出，默认为NO
        annotationView.animatesDrop=YES;         //设置标注动画显示，默认为NO
        //annotationView.draggable = YES;          //设置标注可以拖动，默认为NO
        return annotationView;
    }
    return nil;
}



#pragma mark -- 懒加载

- (BMKUserLocation *)userLocation {
    if (!_userLocation) {
        _userLocation = [[BMKUserLocation alloc] init];
    }
    
    return _userLocation;
}


@end
